import api from '../../api/index'
import * as types from '../mutation-types';

//初始化一个变量
const state = {
  //党建动态
  homeList: [], //动态数据列表
  homeListTotal: 0, //总动态数
  homeDetail: {}, //动态详情
  banners: [],

  //支部风采
  branchList: [], //支部列表
  branchInfo: {}, //支部信息


  //三会一课
  lessonsList: [], //最新动态
  lessonsTotal: 0,
  lessonsBranch: [], //统计分析支部列表
  lessonDetail: {}, //课程详情
  branchLessons: [], //支部课程列表
  branchLessonsTotal: 0,


  //走帮服
  helpsList: [], //走帮服列表
  helpsListTotal: 0,
  helpsBranch: [], //支部列表
  helpsBranchSelect: [],
  helpsPerson: [], //党员列表
  helpsPersonSelect: [],
  personHelps: [], //党员走帮服列表
  helpDetail: {},

  //党费缴纳
  feesBranch: [], //支部缴费
  feesPerson: [], //党员缴费
  feesBranchSelect: [],
  feesPersonSelect: [],

  //党员积分
  integralQuarterList: [], //积分列表
  integraYearlList: [], //积分列表
  integralQuarterSelect: [],
  integraYearSelect: [],
  integralQuarterTotal: 0,
  integraYearlTotal: 0,
  //消息中心
  newsLists: [],
  unNewsNums: 0,

  //出错信息
  errorAnNewsMsg: '',

  homeNewsScrollTop: 0,
  lessonScrollTop: 0,
  lessonBranchScrollTop: 0,
  helpScrollTop: 0,
}

//操作：发送请求
const actions = {
  //党建动态
  getHomeList({
    commit
  }, params) {
    commit('clearNewsMsg')

    api.get_home_lists(params).then(res => {
      commit(types.GET_HOME_LISTS, res)
    });
  },
  getHomeDetail({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_home_detail(params).then(res => {
      commit(types.GET_HOME_DETAIL, res)
    });
  },

  //支部风采
  getBranchList({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_branch_list(params).then(res => {
      commit(types.GET_BRANCH_LISTS, res)
    })
  },
  getBranchInfo({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_branch_infos(params).then(res => {
      commit(types.GET_BRANCH_INFOS, res)
    })
  },

  //三会一课
  getLessonsList({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_lessons_list(params).then(res => {
      commit(types.GET_LESSONS_LIST, res);
    })
  },
  getLessonsBranch({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.search_branch_list(params).then(res => {
      commit(types.GET_LESSONS_BRANCH, res)
    })
  },
  getLessonDetail({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_lesson_detail(params).then(res => {
      commit(types.GET_LESSON_DETAIL, res)
    })
  },
  getBranchLessons({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_branch_lessons(params).then(res => {
      commit(types.GET_BRANCH_LESSONS, res);
    })
  },

  //党费缴纳
  getFeesBranch({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_fees_branch(params).then(res => {
      commit(types.GET_FEES_BRANCH, res)
    })
  },
  getFeesPerson({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_fees_person(params).then(res => {
      commit(types.GET_FEES_PERSON, res)
    })
  },
  getFeesBranchSelect({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_fees_branch_select(params).then(res => {
      commit(types.GET_FEES_BRANCH_SELECT, res)
    })
  },
  getFeesPersonSelect({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_fees_person_select(params).then(res => {
      commit(types.GET_FEES_PERSON_SELECT, res)
    })
  },


  //走帮服
  getHelpsList({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_helps_list(params).then(res => {
      commit(types.GET_HELPS_LIST, res)
    })
  },
  getHelpsBranch({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_helps_branch(params).then(res => {
      commit(types.GET_HELPS_BRANCH, res)
    })
  },
  getHelpsPerson({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_helps_person(params).then(res => {
      commit(types.GET_HELPS_PERSON, res)
    })
  },
  getPersonHelps({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_person_helps(params).then(res => {
      commit(types.GET_PERSON_HELPS, res)
    })
  },
  getHelpDetail({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_help_detail(params).then(res => {
      commit(types.GET_HELP_DETAIL, res)
    })
  },
  getHelpBranchSelect({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_help_branch_select(params).then(res => {
      commit(types.GET_HELP_BRANCH_SELECT, res)
    })
  },
  getHelpPersonSelect({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_help_person_select(params).then(res => {
      commit(types.GET_HELP_PERSON_SELECT, res)
    })
  },

  //党员积分
  getIntegralQuarterList({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_integral_list(params).then(res => {
      commit(types.GET_INTEGRAL_QUARTER_LIST, res)
    })
  },
  getIntegralYearList({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_integral_list2(params).then(res => {
      commit(types.GET_INTEGRAL_YEAR_LIST, res)
    })
  },
  getIntegralQuarterSelect({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_quarter_select(params).then(res => {
      commit(types.GET_QUARTER_SELECT, res)
    })
  },
  getIntegralYearSelect({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_year_select(params).then(res => {
      commit(types.GET_YEAR_SELECT, res)
    })
  },

  //消息中心
  getNewsList({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_news_lists(params).then(res => {
      commit(types.GET_NEWS_LISTS, res)
    })
  },
  getUnNewsTotals({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_unnews_nums(params).then(res => {
      commit(types.GET_NEWS_NUM, res)
    })
  },
  getBanners({
    commit
  }, params) {
    commit('clearNewsMsg')
    api.get_banner_list(params).then(res => {
      commit(types.GET_BANNER_LIST, res)
    })
  },

  updateHomePosition({
    commit
  }, top) {
    commit({
      type: 'updateHomePosition',
      top: top
    })
  },
  updateLessonPosition({
    commit
  }, top) {
    commit({
      type: 'updateLessonPosition',
      top: top
    })
  },
  updateLessonBranchPosition({
    commit
  }, top) {
    commit({
      type: 'updateLessonBranchPosition',
      top: top
    })
  },
  updateHelpPosition({
    commit
  }, top) {
    commit({
      type: 'updateHelpPosition',
      top: top
    })
  }
}

//取变量
const getters = {
  //党建动态
  homeList: state => state.homeList,
  homeListTotal: state => state.homeListTotal,
  homeDetail: state => state.homeDetail,
  banners: state => state.banners,

  //支部风采
  branchList: state => state.branchList,
  branchInfo: state => state.branchInfo,

  //三会一课
  lessonsList: state => state.lessonsList,
  lessonsTotal: state => state.lessonsTotal,
  lessonsBranch: state => state.lessonsBranch,
  lessonDetail: state => state.lessonDetail,
  branchLessons: state => state.branchLessons,
  branchLessonsTotal: state => state.branchLessonsTotal,

  //党费缴纳
  feesBranch: state => state.feesBranch,
  feesPerson: state => state.feesPerson,
  feesBranchSelect: state => state.feesBranchSelect,
  feesPersonSelect: state => state.feesPersonSelect,

  //走帮服
  helpsList: state => state.helpsList,
  helpsListTotal: state => state.helpsListTotal,
  helpsBranch: state => state.helpsBranch,
  helpsBranchSelect: state => state.helpsBranchSelect,
  helpsPerson: state => state.helpsPerson,
  helpsPersonSelect: state => state.helpsPersonSelect,
  personHelps: state => state.personHelps,
  helpDetail: state => state.helpDetail,

  //党员积分
  integralQuarterList: state => state.integralQuarterList,
  integraYearlList: state => state.integraYearlList,
  integraYearSelect: state => state.integraYearSelect,
  integralQuarterSelect: state => state.integralQuarterSelect,
  integralQuarterTotal: state => state.integralQuarterTotal,
  integraYearlTotal: state => state.integraYearlTotal,


  //消息通知
  newsLists: state => state.newsLists,
  unNewsNums: state => state.unNewsNums,
  //出错信息
  errorAnNewsMsg: state => state.errorAnNewsMsg,
  homeNewsScrollTop: state => state.homeNewsScrollTop,
  lessonScrollTop: state => state.lessonScrollTop,
  lessonBranchScrollTop: state => state.lessonBranchScrollTop,
  helpScrollTop: state => state.helpScrollTop,
}

//数据操作
const mutations = {
  //党建动态
  [types.GET_HOME_LISTS](state, res) {
    //如果请求返回的是空，则继续初始化变量
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.homeList = []
      state.homeListTotal = 0
    } else { //请求到数据，对数据进行操作（操作目的？）
      if (res.data.data.length !== 0 && res.data.current_page !== 1) {
        let end = res.data.current_page * 10
        let start = end - 10
        for (start; start < end; start++) {
          if (start < res.data.total) {
            let newIndex = start % 10;
            state.homeList.push(res.data.data[newIndex])
          }
        }
      } else if (res.data.current_page == 1 && res.data.data.length !== 0) {
        state.homeList = res.data.data
        state.homeListTotal = res.data.total
      }
    }
  },
  [types.GET_HOME_DETAIL](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.homeDetail = {}
    } else {
      state.homeDetail = res.data
    }
  },

  //支部风采
  [types.GET_BRANCH_LISTS](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.branchList = []
    } else {
      state.branchList = res.data
    }
  },
  [types.GET_BRANCH_INFOS](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.branchInfo = {}
    } else {
      state.branchInfo = res.data
    }
  },

  //三会一课
  [types.GET_LESSONS_LIST](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.lessonsList = []
      state.lessonsTotal = 0
    } else {
      if (res.data.data.length !== 0 && res.data.current_page !== 1) {
        let end = res.data.current_page * 10
        let start = end - 10
        for (start; start < end; start++) {
          if (start < res.data.total) {
            let newIndex = start % 10;
            state.lessonsList.push(res.data.data[newIndex])
          }
        }
      } else if (res.data.current_page == 1 && res.data.data.length !== 0) {
        state.lessonsList = res.data.data
        state.lessonsTotal = res.data.total
      }
    }
  },
  [types.GET_LESSONS_BRANCH](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.lessonsBranch = []
    } else {
      state.lessonsBranch = res.data
    }
  },
  [types.GET_LESSON_DETAIL](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.lessonDetail = {}
    } else {
      state.lessonDetail = res.data
    }
  },
  [types.GET_BRANCH_LESSONS](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.branchLessons = []
      state.branchLessonsTotal = 0
    } else {
      if (res.data.data.length !== 0 && res.data.current_page !== 1) {
        let end = res.data.current_page * 10
        let start = end - 10
        for (start; start < end; start++) {
          if (start < res.data.total) {
            let newIndex = start % 10;
            state.branchLessons.push(res.data.data[newIndex])
          }
        }
      } else if (res.data.current_page == 1 && res.data.data.length !== 0) {
        state.branchLessons = res.data.data
        state.branchLessonsTotal = res.data.total
      }
    }
  },

  //党费缴纳
  [types.GET_FEES_BRANCH](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.feesBranch = []
    } else {
      state.feesBranch = res.data
    }
  },
  [types.GET_FEES_PERSON](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.feesPerson = []
    } else {
      state.feesPerson = res.data
    }
  },
  [types.GET_FEES_BRANCH_SELECT](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.feesBranchSelect = []
    } else {
      state.feesBranchSelect = res.data
    }
  },
  [types.GET_FEES_PERSON_SELECT](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.feesPersonSelect = []
    } else {
      state.feesPersonSelect = res.data
    }
  },

  //走帮服
  [types.GET_HELPS_LIST](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.helpsList = []
      state.helpsListTotal = 0
    } else {
      if (res.data.data.length !== 0 && res.data.current_page !== 1) {
        let end = res.data.current_page * 10
        let start = end - 10
        for (start; start < end; start++) {
          if (start < res.data.total) {
            let newIndex = start % 10;
            state.helpsList.push(res.data.data[newIndex])
          }
        }
      } else if (res.data.current_page == 1 && res.data.data.length !== 0) {
        state.helpsList = res.data.data
        state.helpsListTotal = res.data.total
      }
    }
  },
  [types.GET_HELPS_BRANCH](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.helpsBranch = []
    } else {
      state.helpsBranch = res.data
    }
  },
  [types.GET_HELPS_PERSON](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.helpsPerson = []
    } else {
      state.helpsPerson = res.data
    }
  },
  [types.GET_PERSON_HELPS](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.personHelps = []
    } else {
      state.personHelps = res.data
    }
  },
  [types.GET_HELP_DETAIL](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.helpDetail = {}
    } else {
      state.helpDetail = res.data
    }
  },
  [types.GET_HELP_BRANCH_SELECT](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.helpsBranchSelect = []
    } else {
      state.helpsBranchSelect = res.data
    }
  },
  [types.GET_HELP_PERSON_SELECT](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.helpsPersonSelect = []
    } else {
      state.helpsPersonSelect = res.data
    }
  },

  //党员积分
  [types.GET_INTEGRAL_QUARTER_LIST](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.integralQuarterList = []
      state.integralQuarterTotal = 0
    } else {
      if (res.data.data.length !== 0 && res.data.current_page !== 1) {
        let end = res.data.current_page * 10
        let start = end - 10
        for (start; start < end; start++) {
          if (start < res.data.total) {
            let newIndex = start % 10;
            state.integralQuarterList.push(res.data.data[newIndex])
          }
        }
      } else if (res.data.current_page == 1 && res.data.data.length !== 0) {
        state.integralQuarterList = res.data.data
        state.integralQuarterTotal = res.data.total
      }
    }
  },
  [types.GET_QUARTER_SELECT](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.integralQuarterSelect = []
    } else {
      state.integralQuarterSelect = res.data.data
    }
  },
  [types.GET_INTEGRAL_YEAR_LIST](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.integraYearlList = []
      state.integraYearlTotal = 0
    } else {
      if (res.data.data.length !== 0 && res.data.current_page !== 1) {
        let end = res.data.current_page * 10
        let start = end - 10
        for (start; start < end; start++) {
          if (start < res.data.total) {
            let newIndex = start % 10;
            state.integraYearlList.push(res.data.data[newIndex])
          }
        }
      } else if (res.data.current_page == 1 && res.data.data.length !== 0) {
        state.integraYearlList = res.data.data
        state.integraYearlTotal = res.data.total
      }
    }
  },
  [types.GET_YEAR_SELECT](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.integraYearSelect = []
    } else {
      state.integraYearSelect = res.data.data
    }
  },

  //消息中心
  [types.GET_NEWS_LISTS](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.newsLists = []
    } else {
      state.newsLists = res.data
    }
  },
  [types.GET_NEWS_NUM](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.unNewsNums = 0
    } else {
      state.unNewsNums = res.data
    }
  },
  [types.GET_BANNER_LIST](state, res) {
    if (res.code === 0) {
      state.errorAnNewsMsg = res.msg
      state.banners = []
    } else {
      state.banners = res.data.data
    }
  },

  clearNewsMsg(state) {
    state.errorAnNewsMsg = ''
  },

  updateHomePosition(state, payload) {
    state.homeNewsScrollTop = payload.top
  },
  updateLessonPosition(state, payload) {
    state.lessonScrollTop = payload.top
  },
  updateLessonBranchPosition(state, payload) {
    state.lessonBranchScrollTop = payload.top
  },
  updateHelpPosition(state, payload) {
    state.helpScrollTop = payload.top
  }
}

export default {
  state,
  actions,
  getters,
  mutations,
}
